package cn.imvc.app.models;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.imvc.core.ICache;

@Service
public class CommonModelImpl implements CommonModel {
	
	@Autowired
	ICache iCache;
	@Autowired
	GoodsModel goodsModel;
	@Autowired
	GoodsClassModel goodsClassModel;
	@Autowired
	GoodsSkuModel goodsSkuModel;
	@Autowired
	ArticleModel articleModel;
	@Autowired
	ArticleClassModel articleClassModel;

	@Override
	public Integer findMaxId(String key) {
		Integer max=0;
		
		if(key==null){return max;}
		if("goods".equals(key)){
			if(iCache.getSecond("MAX_ID", "goods")==null){
				Object object=goodsModel.findMaxId().get(0);
				Integer goodsMaxId=Integer.parseInt(object.toString());
				iCache.putSecond("MAX_ID", "goods", goodsMaxId);
			}
			return iCache.getSecond("MAX_ID", "goods");
		}
		if("goodsClass".equals(key)){
			if(iCache.getSecond("MAX_ID", "goodsClass")==null){
				Object object=goodsClassModel.findMaxId().get(0);
				Integer goodsClassMaxId=Integer.parseInt(object.toString());
				iCache.putSecond("MAX_ID", "goodsClass", goodsClassMaxId);
			}
			return iCache.getSecond("MAX_ID", "goodsClass");
		}
		if("goodsSku".equals(key)){
			if(iCache.getSecond("MAX_ID", "goodsSku")==null){
				Object object=goodsSkuModel.findMaxId().get(0);
				Integer goodsSkuMaxId=Integer.parseInt(object.toString());
				iCache.putSecond("MAX_ID", "goodsSku", goodsSkuMaxId);
			}
			return iCache.getSecond("MAX_ID", "goodsSku");
		}
		if("article".equals(key)){
			if(iCache.getSecond("MAX_ID", "article")==null){
				Object object=articleModel.findMaxId().get(0);
				Integer articleMaxId=Integer.parseInt(object.toString());
				iCache.putSecond("MAX_ID", "article", articleMaxId);
			}
			return iCache.getSecond("MAX_ID", "article");
		}
		if("articleClass".equals(key)){
			if(iCache.getSecond("MAX_ID", "articleClass")==null){
				Object object=articleClassModel.findMaxId().get(0);
				Integer articleClassMaxId=Integer.parseInt(object.toString());
				iCache.putSecond("MAX_ID", "articleClass", articleClassMaxId);
			}
			return iCache.getSecond("MAX_ID", "articleClass");
		}
		return 0;
	}
	
}
